package utility;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.*;

public class Utility {
    public static final int byteNumber = 30;

    public static String getTableName(int table) {
        String tableName = "";
        switch (table) {
            case 1://加密
                tableName = "encryption";
                break;
            case 2://解密
                tableName = "decryption";
                break;
            case 3://验证
                tableName = "verification";
                break;
            case 4://签名
                tableName = "signature";
                break;
            case 5://签密
                tableName = "signcryption";
                break;
            case 6://解签密
                tableName = "unsigncryption";
                break;
            case 7://mac
                tableName = "mac";
                break;
            case 8://认证加密
                tableName = "authenticated_encryption";
                break;
            case 9://解密验证
                tableName = "decryption_verification";
                break;
            default:
        }
        return tableName;
    }

    public static JSONArray sortHashMapByValues(HashMap<String, Integer> passedMap, String column) {
        ArrayList<String> mapKeys = new ArrayList<>(passedMap.keySet());
        ArrayList<Integer> mapValues = new ArrayList<>(passedMap.values());
        //指定降序输出
        mapValues.sort((o1, o2) -> o2 - o1);
        Collections.sort(mapKeys);
        JSONArray jsonArray = new JSONArray();
        int count = 1;

        for (Integer val : mapValues) {
            Iterator<String> keyIt = mapKeys.iterator();
            while (keyIt.hasNext()) {
                String key = keyIt.next();
                int comp1 = passedMap.get(key);
                if (comp1 == val) {
                    JSONObject jsonObject = new JSONObject();
                    keyIt.remove();
                    jsonObject.put("id", count);
                    jsonObject.put(column, key);
                    jsonObject.put("count", val);
                    jsonArray.add(jsonObject);
                    count++;
                    break;
                }
            }
        }
        return jsonArray;
    }
}
